//
//  DetailView.m
//  HappyBaby
//
//  Created by lanou on 15/11/25.
//  Copyright © 2015年 华哥. All rights reserved.
//

#import "HappyDetailView.h"
#import "Define.h"
#import "DetailCollectionCell.h"
#import "GuessLikeModel.h"
@interface HappyDetailView ()

@property (nonatomic, strong) UIView *bgView;

@end

@implementation HappyDetailView
{
    CGFloat _scaleX;
    CGFloat _scaleY;
    CGRect _frame;
}


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _frame = frame;
        // 设置scrollView的一些属性
        //self.showsHorizontalScrollIndicator = NO;
        //self.showsVerticalScrollIndicator = NO;
        self.backgroundColor = [UIColor whiteColor];
        self.contentSize = CGSizeMake(kScreenWidth,kScreenHeight * 2);
        self.contentOffset = CGPointMake(200, 0);
        self.bounces = NO;
        
        // 创建imageView
        _scaleY = kScreenHeight / 667;
        CGFloat imageH = kScreenWidth * 200 /320 * _scaleY;
        
        self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, imageH)];
        self.imageView.backgroundColor = [UIColor magentaColor];
        [self addSubview:_imageView];
        
        UIView *blackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, imageH)];
        blackView.backgroundColor = [UIColor blackColor];
        blackView.alpha = 0.2;
        [self.imageView addSubview:blackView];
        
        // 创建titlelabel
        _scaleX = kScreenWidth / 375;
        CGFloat X = 20 * _scaleX;
        self.titleLable = [[UILabel alloc] initWithFrame:CGRectMake(X, imageH - 30 * _scaleY, kScreenWidth - X, 25 *_scaleY)];
        self.titleLable.textColor = [UIColor whiteColor];
        self.titleLable.text = @"大型三明治--建立不同顺序关系";
        [self.imageView addSubview:self.titleLable];
        
        // 创建webView
        self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(10 * _scaleX, self.imageView.height + 20 * _scaleY, kScreenWidth - 20 * _scaleX, 100)];
        self.webView.scrollView.scrollEnabled = NO;
        [self addSubview:self.webView];
        
        
        // 猜你喜欢
        CGFloat likeImageH = 120 * 226 / 1094;
        
        UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, self.webView.y + self.webView.height + 30 * _scaleY, kScreenWidth, likeImageH + 8)];
        self.bgView = bgView;
        [self addSubview:bgView];
        
        
        UIImageView *likeImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20*_scaleX, 4, 120*_scaleX, likeImageH)];
        likeImageView.image = [UIImage imageNamed:@"cainixihuan"];
        [bgView addSubview:likeImageView];
        UIView *line1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 2)];
        line1.backgroundColor = [UIColor lightGrayColor];
        [bgView addSubview:line1];
        
        UIView *line2 = [[UIView alloc] initWithFrame:CGRectMake(0, bgView.height - 2, kScreenWidth, 2)];
        line2.backgroundColor = [UIColor lightGrayColor];
        [bgView addSubview:line2];
        
        // 创建collectionView
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        CGFloat collectionW = kScreenWidth - 40 * _scaleX;
       
        CGFloat itemW = (collectionW - 20 * _scaleX)/2;
        layout.itemSize = CGSizeMake(itemW, itemW);
        layout.minimumInteritemSpacing = 20;
        layout.minimumLineSpacing = 20;
        self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(20 * _scaleX, bgView.y + bgView.height + 30*_scaleY, collectionW, collectionW) collectionViewLayout:layout];
        self.collectionView.showsHorizontalScrollIndicator = NO;
        self.collectionView.showsVerticalScrollIndicator = NO;
        self.collectionView.backgroundColor = [UIColor whiteColor];
        self.collectionView.delegate = self;
        self.collectionView.dataSource = self;
        
        // 注册cell
        [self.collectionView registerClass:[DetailCollectionCell class] forCellWithReuseIdentifier:@"CollectionCell"];
        
        [self addSubview:self.collectionView];
          
    }
    return self;
}

#pragma mark --修改frame 和scrollView的contentSize
- (void)changeDetailViewFrameWithwebViewHeight:(CGFloat)height
{
    self.webView.height = height + 30 * _scaleX;
    self.bgView.y = self.webView.y + self.webView.height + 30 * _scaleY;
    self.collectionView.y = _bgView.y + _bgView.height + 30*_scaleY;
    self.contentSize = CGSizeMake(_frame.size.width, self.collectionView.y + self.collectionView.height + 50*kScaleY);
}


#pragma mark --返回分区下item数量
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.guessLikeArray.count;
}
#pragma mark --返回cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    DetailCollectionCell *cell = (DetailCollectionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionCell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor greenColor];
    cell.layer.cornerRadius = 10;
    cell.layer.masksToBounds = YES;
    
    GuessLikeModel *model = self.guessLikeArray[indexPath.item];
    cell.guessLikeModel = model;
    return cell;
}

#pragma mark --选中cell的方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    GuessLikeModel *model = self.guessLikeArray[indexPath.item];
    [self.delegater pushtoViewControllerWithModel:model];
}

@end
